package Array;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-26  09:12
 * @Description: TODO
 * @Version: 1.0
 */
public class 有序数组的平方977 {

    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int index = result.length - 1;
        int left = 0;
        int right = nums.length - 1;
        // 当相等于的时候一定是需要
        while (left <= right) {
            if (nums[left] * nums[left] >= nums[right] * nums[right]) {
                result[index--] = nums[left] * nums[left];
                left++;
            } else {
                result[index--] = nums[right] * nums[right];
                right--;
            }
        }
        return result;
    }
}
